package com.msh.platform.elm.service.impl;

import com.msh.platform.bizmedia.base.util.Tools;
import com.msh.platform.elm.common.constants.ExamineType;
import com.msh.platform.elm.common.constants.WxPubNoStatus;
import com.msh.platform.elm.common.util.MapTrunPojoUtils;
import com.msh.platform.elm.dao.OperLogRepository;
import com.msh.platform.elm.dao.SaleDqOptRepository;
import com.msh.platform.elm.dao.SaleDqRepository;
import com.msh.platform.elm.dao.SaleInfoRepository;
import com.msh.platform.elm.dao.WxPubNoExamRepository;
import com.msh.platform.elm.dao.WxPubNoRepository;
import com.msh.platform.elm.entity.OperLog;
import com.msh.platform.elm.entity.SaleDq;
import com.msh.platform.elm.entity.SaleDqOpt;
import com.msh.platform.elm.entity.SaleInfo;
import com.msh.platform.elm.entity.WxPubNo;
import com.msh.platform.elm.entity.WxPubNoExam;
import com.msh.platform.elm.service.OperLogService;
import com.msh.platform.elm.service.WxPubNoService;
import com.msh.platform.elm.vo.ExamPubNoVO;
import com.msh.platform.elm.vo.PubNoDqVO;
import com.msh.platform.elm.vo.WxPubNoExamVO;
import com.msh.platform.elm.vo.WxPubNoRqVO;
import com.msh.platform.elm.vo.WxPubNoRsVO;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class WxPubNoServiceImpl implements WxPubNoService {

    @Autowired
    private WxPubNoRepository wxPubNoRepository;
    @Autowired
    private SaleDqRepository saleDqRepository;
    @Autowired
    private SaleDqOptRepository saleDqOptRepository;
    @Autowired
    private SaleInfoRepository saleInfoRepository;
    @Autowired
    private WxPubNoExamRepository wxPubNoExamRepository;
    @Autowired
    private OperLogRepository operLogRepository;
    @Autowired
    private OperLogService operLogService;
    
    public Page<WxPubNo> findBySellerId(int sellerId,Pageable pageable){
    	return wxPubNoRepository.findBySellerId(sellerId, pageable);
    }
    
    public Page<WxPubNo> findAll(Pageable pageable){
    	return wxPubNoRepository.findAll(pageable);
    }
    
    
    public WxPubNo save(WxPubNo wxPubNo) {
    	return wxPubNoRepository.save(wxPubNo);
    }
    
    //新增公众号
    @Transactional
    public void addWxPubNo(WxPubNoRqVO vo, int sellerId) {
    	//保存公众号基本信息
    	Integer currTime = Tools.getCurrTime();
		WxPubNo wxPubNo = new WxPubNo();
		BeanUtils.copyProperties(vo,wxPubNo);
		wxPubNo.setSellerId(sellerId);
		wxPubNo.setStatus("0");
		wxPubNo.setCreateTime(currTime);
		wxPubNo.setStateTime(currTime);
		wxPubNo.setDelFlag("0");
		int pubNoId = wxPubNoRepository.save(wxPubNo).getId();
		//保存公众号档期信息
		saveDqInfo(vo,pubNoId);
		
		//新增操作日志
		operLogService.addOperLog(pubNoId, "上架", "申请公众号上架", sellerId);
    }
    
    //获取公众号基本信息
    public WxPubNo getWxPubNo(int id) {
    	WxPubNo wxPubNo = wxPubNoRepository.getOne(id);
        return wxPubNo;
    }
    
    //获取公众号及档期信息
    public WxPubNoRsVO getWxPubNoVO(int id) {
    	WxPubNo wxPubNo = wxPubNoRepository.getOne(id);
    	WxPubNoRsVO wxPubNoRsVO = new WxPubNoRsVO();
		BeanUtils.copyProperties(wxPubNo,wxPubNoRsVO);
		List<PubNoDqVO> dqVoList = new ArrayList<PubNoDqVO>();
		
		List<SaleInfo> saleInfoList = saleInfoRepository.findByPubNoId(id);
		for(SaleInfo saleInfo : saleInfoList) {
			SaleDq dq = saleDqRepository.getOne(saleInfo.getDqId());  //查询档期信息
			SaleDqOpt dqOpt = saleDqOptRepository.findByDqId(saleInfo.getDqId());  //查询档期特殊设置信息
			
			PubNoDqVO dqVO = new PubNoDqVO();
			dqVO.setSpecId(saleInfo.getSpecId());  //版位id
			dqVO.setPrice(saleInfo.getPrice());
			dqVO.setDqType(dq.getType());
			dqVO.setDq(dq.getDq());
			dqVO.setAddDay(dqOpt.getAddDay());
			dqVO.setDelDay(dqOpt.getDelDay());
			dqVoList.add(dqVO);
		}
		wxPubNoRsVO.setDqs(dqVoList);
		return wxPubNoRsVO;
    }
    
    //修改公众号
    @Transactional
    public void editWxPubNo(WxPubNoRqVO vo,int id,int sellerId) {
    	WxPubNo oriWxPubNo = wxPubNoRepository.getOne(id); //获取原来的公众号基本信息
    	
    	WxPubNoExam wxPubNoExam = new WxPubNoExam();  //修改基本信息后的实体对象
    	BeanUtils.copyProperties(vo,wxPubNoExam);
    	wxPubNoExam.setId(id);
    	
    	WxPubNoExam oriWxPubNoExam = new WxPubNoExam();  //原来的基本信息实体对象
    	BeanUtils.copyProperties(oriWxPubNo,oriWxPubNoExam);
    	
    	//保存公众号基本信息
    	Integer currTime = Tools.getCurrTime();
		WxPubNo wxPubNo = new WxPubNo();
		BeanUtils.copyProperties(vo,wxPubNo);
		wxPubNo.setId(id);
		wxPubNo.setStateTime(currTime);
		wxPubNo.setCreateTime(oriWxPubNo.getCreateTime());
		wxPubNo.setDelFlag("0");
		if(!MapTrunPojoUtils.compareObj(wxPubNoExam, oriWxPubNoExam)) {  //如果有修改公众号基本信息
			wxPubNoRepository.updateStatus("3", id);  //把状态改为“已上架，修改中”
			
			//保存修改后的基本信息到修改审核表里
			wxPubNoExamRepository.save(wxPubNoExam);
			
			//新增操作日志
			operLogService.addOperLog(id, "修改", "申请公众号信息修改", sellerId);
    	}
		
		//先删除原来的所有版位档期信息
		List<SaleInfo> saleInfoList = saleInfoRepository.findByPubNoId(id);  //查询公众号的所有销售版位
		List<SaleDq> saleDqList = new ArrayList<SaleDq>();  //公众号的版位档期记录
		List<SaleDqOpt> dqOptList = new ArrayList<SaleDqOpt>();  //公众号的档期特殊设置记录
		for(SaleInfo saleInfo : saleInfoList) {
			SaleDq saleDq = saleDqRepository.getOne(saleInfo.getDqId());
			saleDqList.add(saleDq);
			
			SaleDqOpt dqOpt = saleDqOptRepository.findByDqId(saleInfo.getDqId());
			dqOptList.add(dqOpt);
		}
		
		saleDqOptRepository.deleteInBatch(dqOptList);  //删除原来的所有档期特殊设置记录
		saleDqRepository.deleteInBatch(saleDqList);  //删除原来的所有档期记录
		saleInfoRepository.deleteInBatch(saleInfoList);  //删除原来的所有销售版位记录
		
		//再新增新的版位档期信息
		saveDqInfo(vo,id);
    }
    
    //保存公众号档期信息
    public void saveDqInfo(WxPubNoRqVO vo,int pubNoId) {
    	Integer currTime = Tools.getCurrTime();
    	List<PubNoDqVO> dqList = vo.getDqs();
		for(PubNoDqVO dqVo : dqList)
		{
			//保存档期信息
			SaleDq dq = new SaleDq();
			dq.setType(dqVo.getDqType());
			dq.setDq(dqVo.getDq());
			dq.setCreateTime(currTime);
			int dqId = saleDqRepository.save(dq).getId();
			
			//保存档期特殊设置信息
			if(StringUtils.isNotBlank(dqVo.getAddDay()) || StringUtils.isNotBlank(dqVo.getDelDay())) {
				SaleDqOpt dqOpt = new SaleDqOpt();
				dqOpt.setDqId(dqId);
				dqOpt.setAddDay(dqVo.getAddDay());
				dqOpt.setDelDay(dqVo.getDelDay());
				saleDqOptRepository.save(dqOpt);
			}
			
			//保存档期销售信息
			SaleInfo saleInfo = new SaleInfo();
			saleInfo.setPubNoId(pubNoId);
			saleInfo.setSpecId(dqVo.getSpecId());
			saleInfo.setDqId(dqId);
			saleInfo.setPrice(dqVo.getPrice());
			saleInfo.setCreateTime(currTime);
			saleInfoRepository.save(saleInfo);
		}
	}
    
    @Transactional
    public void examPubNo(ExamPubNoVO vo, int sellerId) {
    	WxPubNo wxPubNo = wxPubNoRepository.getOne(vo.getId());
		
		OperLog firstOperLog = operLogRepository.findFirstByOperObjOrderByCreateTimeDesc(vo.getId());  //获取提交审核的最新操作日志
		String operType = firstOperLog.getOperType();  //获取最新的操作类型
		ExamineType examType = ExamineType.getEnumByCode(vo.getType());  //根据传入的审核类型代码获取对应的枚举项
		if("上架".equals(operType) || "重新上架".equals(operType)) {  //申请上架或重新上架
			switch (examType) {
				case pass:  //审核通过
					wxPubNo.setStatus(WxPubNoStatus.putOn.getCode());  //上架
					wxPubNoRepository.save(wxPubNo);
					//新增操作日志
					operLogService.addOperLog(vo.getId(), "审核通过", "申请上架审核通过", sellerId);
					break;
				case noPass:  //审核不通过
					wxPubNo.setStatus(WxPubNoStatus.noPass.getCode());  //不通过
					wxPubNoRepository.save(wxPubNo);
					//新增操作日志
					operLogService.addOperLog(vo.getId(), "审核不通过", vo.getReason(), sellerId);
					break;
				default:
					break;
			}
		}
		if("下架".equals(operType)) {  //申请下架
			switch (examType) {
				case pass:  //审核通过
					wxPubNo.setStatus(WxPubNoStatus.pullOff.getCode());  //下架
					wxPubNoRepository.save(wxPubNo);
					//新增操作日志
					operLogService.addOperLog(vo.getId(), "审核通过", "申请下架审核通过", sellerId);
					break;
				case noPass:  //审核不通过
					operLogService.addOperLog(vo.getId(), "审核不通过", vo.getReason(), sellerId);
					break;
				default:
					break;
			}
		}
		if("修改".equals(operType)) {  //申请修改信息
			switch (examType) {
				case pass:  //审核通过
					wxPubNo.setStatus(WxPubNoStatus.putOn.getCode());  //将状态改为已上架
					//将提交审核的修改信息覆盖到上架的信息
					WxPubNoExam wxPubNoExam = wxPubNoExamRepository.getOne(vo.getId());
					WxPubNoExamVO wxPubNoExamVO = new WxPubNoExamVO();
					BeanUtils.copyProperties(wxPubNoExam, wxPubNoExamVO);
					BeanUtils.copyProperties(wxPubNoExamVO, wxPubNo);
					wxPubNoRepository.save(wxPubNo);
					operLogService.addOperLog(vo.getId(), "审核通过", "申请修改信息审核通过", sellerId);
					break;
				case noPass:  //审核不通过
					wxPubNo.setStatus(WxPubNoStatus.putOn.getCode());  //将状态改为已上架
					wxPubNoRepository.save(wxPubNo);
					operLogService.addOperLog(vo.getId(), "审核不通过", vo.getReason(), sellerId);
					break;
				default:
					break;
			}
		}
    }

}
